<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function getJSONP({ url, params = {}, callback = `callback${+ new Date()}` }) {
            // DOM
            return new Promise((resolve, reject) => {
                let script = document.createElement('script')
                params = { ...params, callback }
                // queryString的对象  ?callback=show&a=1&b=2
                let arr = [];
                for (let key in params) {
                    arr.push(`${key}=${params[key]}`)
                }
                console.log(arr);
                // queryString 以？开始
                script.src = `${url}?${arr.join('&')}`;
                window[callback] = function (data) {
                    resolve(data);
                }
                document.body.appendChild(script)
            })
        }
        getJSONP({
            url: 'http://localhost:3000/say',
            params: { // 查询参数
                wd: 'I love you'
            },
            callback: 'show' // 必须的
        }).then(data => {
            console.log(data)
        })
    </script>
</body>

</html>